package shuogg.xstream.demo;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

import com.thoughtworks.xstream.XStream;

/**
 * @description XStream专业javaBean转xml
 *
 * @author linshuo
 *
 */
@SuppressWarnings(value = { "all" })
public class XStreamTest {
    //@Test
    public void test() throws Exception {
        OutXmlClassAlias();
    }

    /**
     * @description XStream最简单的例程
     */
    private void OutXmlSimple() {
        List<ClassRoom> classRooms = getClassRoomList();
        XStream xstream = new XStream();
        String xmlStr = xstream.toXML(classRooms);
        printAndSave(xmlStr);
    }

    /**
     * @description 修改标签别名
     */
    private void OutXmlClassAlias() {
        List<ClassRoom> classRooms = getClassRoomList();
        XStream xstream = new XStream();
        // 跟OutXmlSimple的输出对比一下效果明白了
        xstream.alias("student", Student.class); // 标签别名
        xstream.alias("Class", ClassRoom.class);
        xstream.alias("School", List.class);
        String xmlStr = xstream.toXML(classRooms);
        printAndSave(xmlStr);
    }

    /**
     * @description 标签转为属性
     */
    private void OutXmlAttribute() {
        List<ClassRoom> classRooms = getClassRoomList();
        XStream xstream = new XStream();
        xstream.alias("student", Student.class);
        xstream.alias("Class", ClassRoom.class);
        xstream.alias("School", List.class);

        xstream.useAttributeFor(Student.class, "name"); // 把name标签转为name属性
        String xmlStr = xstream.toXML(classRooms);
        printAndSave(xmlStr);
    }

    /**
     * @description 删除无用的标签
     */
    private void OutXmlDeleteLabel() {
        List<ClassRoom> classRooms = getClassRoomList();
        XStream xstream = new XStream();
        xstream.alias("student", Student.class);
        xstream.alias("Class", ClassRoom.class);
        xstream.alias("School", List.class);

        xstream.addImplicitCollection(ClassRoom.class, "students"); // 把List集合的标签隐藏掉
        xstream.omitField(ClassRoom.class, "studentCnt"); // 删掉studentCnt标签
        String xmlStr = xstream.toXML(classRooms);
        printAndSave(xmlStr);
    }

    /**
     * @description println并保存输出为xml文件
     * @param xmlStr
     */
    private void printAndSave(String xmlStr) {
        System.out.println(xmlStr);
        PrintWriter pw;
        try {
            pw = new PrintWriter(new File("F:/xstreamXml.xml"));
            pw.print(xmlStr);
            pw.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * @description 获取bean的list
     * @return
     */
    private List<ClassRoom> getClassRoomList() {
        List<ClassRoom> classRooms = new ArrayList<ClassRoom>();
        Student stu1 = new Student();
        stu1.setAge(12);
        stu1.setName("xiaoming");
        Student stu2 = new Student();
        stu2.setAge(23);
        stu2.setName("linshuo");
        Student stu3 = new Student();
        stu3.setAge(30);
        stu3.setName("xiaohong");

        ClassRoom cls1 = new ClassRoom();
        cls1.setStudentCnt(66);
        cls1.setClassNumber("603");
        cls1.addStudent(stu1);
        cls1.addStudent(stu2);
        cls1.addStudent(stu3);

        classRooms.add(cls1);

        return classRooms;
    }

    /**************************************** XStreamUtils的测试 *************************************************/
    @Test
    public void xstreamUtilsTest() {
        Object testBean = getClassRoomList();
        String baseXmlStr = XStreamUtils.getXmlStrByBean(testBean);
        Class[] classList = new Class[] { List.class, Student.class, ClassRoom.class };
        String[] newNameList = new String[] { "school", "student", "classroom" };
        String xmlStrImprove = null;
        try {
            xmlStrImprove = XStreamUtils.getXmlStrByBeanAlterClassname(testBean, classList, newNameList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(xmlStrImprove);
        XStreamUtils.saveAsXmlfile(xmlStrImprove, "F:/mytest.xml");
    }

}
